package com.szewec.ps.util.watermark;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.media.ExifInterface;

import com.szewec.ps.base.BaseApplication;
import com.szewec.ps.util.DateUtil;
import com.szewec.ps.util.LogUtil;
import com.szewec.ps.util.PreferincesContext;
import com.szewec.ps.util.SharedPreferencesUtil;
import com.watermark.androidwm.Watermark;
import com.watermark.androidwm.WatermarkBuilder;
import com.watermark.androidwm.bean.WatermarkText;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class WatermarkUtil {

    public static String addWaterMark(String mFilePath,boolean needCopy) {
        String address = (String) SharedPreferencesUtil.getData(PreferincesContext.BD_ADDRESS,"暂无地址");
        if (address.contains("国")){
            address = address.substring(2);
        }
        if (address.equals("null")){
            address = "暂无地址";
        }
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(mFilePath);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        LogUtil.i("图片旋转了：" ,degree + " 度");

        Bitmap p1 = BitmapFactory.decodeFile(mFilePath);
        try {
            if (degree != 0){
                Matrix matrix = new Matrix();
                matrix.postRotate(degree);
                // 创建新的图片
                p1 = Bitmap.createBitmap(p1, 0, 0, p1.getWidth(), p1.getHeight(), matrix, true);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        WatermarkText watermarkText = new WatermarkText(DateUtil.convertDateToString(new Date(),"yyyy-MM-dd HH:mm:ss"))
                .setPositionX(0.05)
                .setPositionY(0.9)
                .setTextColor(Color.WHITE)
                .setTextAlpha(255)
                .setTextShadow(0.1f, 1, 1, Color.BLACK)
                .setTextSize(12);
        //去掉中国两字
        WatermarkText watermarkText2 = new WatermarkText(address)
                .setPositionX(0.05)
                .setPositionY(0.95)
                .setTextColor(Color.WHITE)
                .setTextAlpha(255)
                .setTextShadow(0.1f, 1, 1, Color.BLACK)
                .setTextSize(12);
        List<WatermarkText> watermarkTextList = new ArrayList<>();
        watermarkTextList.add(watermarkText);
        watermarkTextList.add(watermarkText2);
        Watermark watermark =  WatermarkBuilder
                .create(BaseApplication.getContext(), p1)
                .loadWatermarkTexts(watermarkTextList)
                .getWatermark();
        Bitmap bitmap = watermark.getOutputImage();
        try {
            if (needCopy){
                String [] p = mFilePath.split("\\.");
                mFilePath = p[0] + "_副本." + p[1];
                LogUtil.i("复制的文件路径:",mFilePath);
                File file = new File(mFilePath);
                file.createNewFile();
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
                return mFilePath;
            }else {
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(mFilePath));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
